# coding: cp1252
'''
Created on 28/03/2009

@author: Neper
'''
import os
from BLL.DAL.DvdShareDC import TbDVD
import BLL.DvdBLL
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

from google.appengine.ext.webapp import template

# The main page where the user can login and logout
# MainPage is a subclass of webapp.RequestHandler and overwrites the get method
class Inicio(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        url = users.create_login_url(self.request.uri)   
        raw_id = int(self.request.get('id'))                             
        if user:
            url = users.create_logout_url(self.request.uri)            
# GQL is similar to SQL
            
        dvd = TbDVD.get_by_id(raw_id)             
        #, author=users.get_current_user())        
        values = {
            'dvd': dvd,            
            'user': user,
            'url': url,
            'acao': 'Reservar',
            'erro':'',
            'youtube':'',
        }
        template_file = os.path.join(os.path.dirname(__file__), 'templates','viewDVD.html')        
        rendered = webapp.template.render(template_file, values)
        self.response.out.write(rendered)           



class Email(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            raw_id = self.request.get('id')
            id = int(raw_id)
            #BLL.DvdBLL.EnviarEmail(id)
            self.redirect('/')  

class Devolver(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            raw_id = self.request.get('id')
            id = int(raw_id)
            #BLL.DvdBLL.EnviarEmail(id)
            self.redirect('/')  

class Reservar(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            raw_id = self.request.get('id')
            id = int(raw_id)
            #BLL.DvdBLL.EnviarEmail(id)
            self.redirect('/')    
            
class Locar(webapp.RequestHandler):
    def get(self):
        user = users.get_current_user()
        if user:
            raw_id = self.request.get('id')
            id = int(raw_id)
            #BLL.DvdBLL.EnviarEmail(id)
            self.redirect('/locacal/info')    

# Register the URL with the responsible classes
application = webapp.WSGIApplication(
                                     [('/locacao/info', Inicio),
                                      ('/locacao/locar', Locar),
                                      ('/locacao/reservar', Reservar),
                                      ('/locacao/devolver', Devolver),
                                      ('/locacao/email', Email)],
                                      debug=True)



# Register the wsgi application to run
def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()